package demo1;

import javax.swing.tree.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:判断一棵树是不是完全二叉树
 * User: liangyuanzhang
 * Date: 2025-4-26
 * Time: 23:56
 */
//如果队列里面有非空的节点 --> 不是完全二叉树
public class Test {



    public boolean isTree(TreeNode root) {

        if (root == null) {
            return true;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);//把根节点入队

        while (!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if (cur != null){
                queue.offer(cur.left);
                queue.offer(cur.right);
            }else {
                break;
            }
        }

        while (!queue.isEmpty()) {
            TreeNode peek = queue.peek();
            if (peek != null){
                return false;
            }
            queue.poll();
        }

        return true;
    }




}
